package code.tools.commponents.button
{
	import code.structure.manager.loader.DllLoaderManager;
	import code.structure.manager.sound.MySoundManager;
	import code.tools.commponents.Component;
	import code.tools.commponents.Helpers;
	import code.tools.commponents.Style;
	import code.tools.sound.src.SoundName;
	import code.untils.LoaderTypeConst;
	
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class IconButton extends Component
	{
		private var normal:int;
		private var hover:int;
		/**
		 * 按钮选中时的文字bmd
		 */
		private var select:int;
		private var _over:Boolean;
		private var _toggle:Boolean;
		protected var _selected:Boolean;
		/**
		 *  onClick(this);
		 */
		private var onClick:Function;
		
		private var mc:Sprite;
		private var _loadCompleteCallBack:Function;
		public function IconButton(parent:DisplayObjectContainer, xpos:Number, ypos:Number,defaultHandler:Function,loadCompleteCallBack:Function = null)
		{
			super(parent , xpos , ypos);
			_loadCompleteCallBack = loadCompleteCallBack;
			onClick = defaultHandler;
		}
		
		override protected function init():void
		{
			super.init();
			buttonMode = true;
			mouseChildren = false;
		}
		
		override protected function addChildren():void
		{
			addEventListener(MouseEvent.ROLL_OVER , overHandler);
			addEventListener(MouseEvent.MOUSE_DOWN , downHandler);
			//			addEventListener(MouseEvent.CLICK , clickHandler);
		}
		
		override public function dispose(... rest):void
		{
			super.dispose();
			mouseChildren = mouseEnabled = false;
			onClick = null;
			removeEventListener(MouseEvent.MOUSE_UP , upHandler);
			removeEventListener(MouseEvent.ROLL_OVER , overHandler);
			removeEventListener(MouseEvent.MOUSE_DOWN , downHandler);
		}
		
		override public function draw():void
		{
			super.draw();
			if(mc == null)return;
//			if(_over)
//				filters = Style.HOVER_FILTERS;
//			else
//				filters = null;
			
			if (_toggle)
			{
				if (_selected)
				{
					TweenLite.killTweensOf(this,true);
//					parent.addChild(this);
					TweenLite.to(this,0.15,{scaleX:1, scaleY:1});
				}
				else
				{
					if(_over)
					{
						TweenLite.killTweensOf(this,true);
//						parent.addChild(this);
						TweenLite.to(this,0.15,{scaleX:1, scaleY:1});
					}
					else
					{
						TweenLite.killTweensOf(this,true);
						TweenLite.to(this,0.15,{scaleX:.9, scaleY:.9});
					}
				}
			}
			else
			{
				if(_over)
				{
					TweenLite.killTweensOf(this,true);
//					parent.addChild(this);
					TweenLite.to(this,0.15,{scaleX:1, scaleY:1});
				}
				else
				{
					TweenLite.killTweensOf(this,true);
					TweenLite.to(this,0.15,{scaleX:.9, scaleY:.9});
				}
			}
		}
		
		private var _mc:Sprite;
		private var _path:String;
		private function clearIcon():void
		{
			if(mc && this.contains(mc))
			{
				removeChild(mc);
				mc = null;
			}
		}
	
		/**
		 *设置BitmapData
		 */
		public function setAppearance(path:String):void
		{
			_path = path;
			clearIcon();
			DllLoaderManager.getInstance().getMaterial(path,'',loadCompleteHandler,'png',LoaderTypeConst.LOADER_TYPE_ICON);
		}
		
		private function loadCompleteHandler(c:*,key:String = ''):void
		{
			if(_path + '&' != key && _path + '&ICON' != key)return;
			clearIcon();
			_mc = Helpers.drawBmd(c,_mc);
			baseRollOutHandler(null);
			this.mc = _mc;
			addChild(mc);
			mc.x = -mc.width * .5;
			mc.y = -mc.height * .5;
			if (mc)
				setSize(mc.width , mc.height);
			invalidate();
			dispatchEvent(new Event(Event.RESIZE));
			if(_loadCompleteCallBack != null)
				_loadCompleteCallBack();
		}
		protected function overHandler(event:MouseEvent):void
		{
			_over = true;
			invalidate();
			MySoundManager.playSound(SoundName.MOUSE_OVER, 0.2);
			addEventListener(MouseEvent.ROLL_OUT , onMouseOut);
		}
		
		/**
		 * Internal mouseOut handler.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onMouseOut(event:MouseEvent):void
		{
			removeEventListener(MouseEvent.ROLL_OUT , onMouseOut);
			if(_selected)return;
			_over = false;
			invalidate();
		}
		
		public var onMouseDown:Function;
		
		private function downHandler(e:MouseEvent):void
		{
			if(_selected)return;
			addEventListener(MouseEvent.MOUSE_UP , upHandler);
			if (onMouseDown != null)
				onMouseDown(this);
		}
		
		protected function upHandler(e:MouseEvent):void
		{
			removeEventListener(MouseEvent.MOUSE_UP , upHandler);
			if (_toggle)
			{
				_selected = true;
				_over = true;
			}
			invalidate();
			onClick(this);
			e.stopImmediatePropagation();
		}
	
		public function get selected():Boolean
		{
			return _selected;
		}
		
		public function set selected(value:Boolean):void
		{
			if (!_toggle)
				value = false;
			if (_selected == value)
				return;
			_selected = value;
			if(value)
				_over = true;
			else
				_over = false;
			invalidate();
		}

		public function get toggle():Boolean
		{
			return _toggle;
		}

		public function set toggle(value:Boolean):void
		{
			_toggle = value;
		}

	}
}